﻿using System;
using System.Text;
using System.Runtime.InteropServices;

namespace BMproductions.Media.MediaPlayer.WMPPlus {
	public static class WMPUtils {
		[DllImport(WMPPlusPublic.WMPPlusHookName, CharSet = CharSet.Auto, EntryPoint = "#301")]
		public static extern IntPtr FindApplicationWindow(
			[MarshalAs(UnmanagedType.LPTStr)]string className,
			[MarshalAs(UnmanagedType.U4)] int styles);

		public static int Rating2Starts(int rating) {
			if (rating >= 38) {
				if (rating < 38 + 25)
					return 3;
				if (rating < 38 + 25 + 24)
					return 4;
				if (rating < 38 + 25 + 24 + 13)
					return 5;
			} else if (rating >= 1) {
				if (rating < 1 + 12)
					return 1;
				if (rating < 1+ 12 + 25)
					return 2;
			}
			return 0;
		}

		public static int GetMajorVersion(string version) {
			var vers = version.Split(new char[] { '.' }, 2);
			if (vers.Length < 2)
				return 0;
			int ver;
			if (!int.TryParse(vers[0], out ver))
				return 0;
			return ver;
		}

		public static void Free(this IDisposable obj) {
			if (obj != null)
				obj.Dispose();
		}

		public static bool IsNullEmpty(this string str) {
			return String.IsNullOrEmpty(str);
		}

		public static string EmptyIfNull(this string str) {
			return str ?? String.Empty;
		}

		public static string NullIfEmpty(this string str) {
			return String.IsNullOrEmpty(str) ? null : str;
		}
	}
}
